package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.dao.WebMapper;
import top.maof.book.model.Web;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.WebService;
import top.maof.book.utils.Utils;

@Service("webServiceImpl")
public class WebServiceImpl extends ServiceImpl<WebMapper, Web> implements WebService {
    @Autowired
    private WebMapper webMapper;

    @Override
    public PageData<Web> findPage(PageWrap<Web> pageWrap) {
        IPage<Web> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Web> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Web::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getContentsSelect() != null) {
            queryWrapper.lambda().eq(Web::getContentsSelect, pageWrap.getModel().getContentsSelect());
        }
        if (pageWrap.getModel().getChapterTitleSelect() != null) {
            queryWrapper.lambda().eq(Web::getChapterTitleSelect, pageWrap.getModel().getChapterTitleSelect());
        }
        if (pageWrap.getModel().getChapterContentSelect() != null) {
            queryWrapper.lambda().eq(Web::getChapterContentSelect, pageWrap.getModel().getChapterContentSelect());
        }
        if (pageWrap.getModel().getChapterNextSelect() != null) {
            queryWrapper.lambda().eq(Web::getChapterNextSelect, pageWrap.getModel().getChapterNextSelect());
        }
        if (pageWrap.getModel().getChapterPreviousSelect() != null) {
            queryWrapper.lambda().eq(Web::getChapterPreviousSelect, pageWrap.getModel().getChapterPreviousSelect());
        }
        if (pageWrap.getModel().getSite() != null) {
            queryWrapper.lambda().eq(Web::getSite, pageWrap.getModel().getSite());
        }
        if (pageWrap.getModel().getUrl() != null) {
            queryWrapper.lambda().eq(Web::getUrl, pageWrap.getModel().getUrl());
        }
        if (pageWrap.getModel().getCharset() != null) {
            queryWrapper.lambda().eq(Web::getCharset, pageWrap.getModel().getCharset());
        }
        for (PageWrap.SortData sortData : pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(webMapper.selectPage(page, queryWrapper));
    }

    public int add(Web web) {
        return webMapper.insert(web);
    }

    public Web find(int id) {
        return webMapper.selectByPrimaryKey(id);
    }

    public int update(Web web) {
        return webMapper.updateByPrimaryKeySelective(web);
    }

    public int delete(int id) {
        return webMapper.deleteByPrimaryKey(id);
    }
}
